﻿using UnityEngine;
using System.Collections;

public static class Tappa {
	public static TappaGame game;
}

public class TappaGame : MonoBehaviour {        void Start() {/*Tappa.game = this;*/ create ();}
		
	public Data_Main data;
	public GameObject liveObjectsFolder;

	public int barsmax = 10;
	public int beersmax = 10;
	public int peepsmax = 10;
	public int tipsmax = 10;

	// Update is called once per frame	
	public bool go = true;
	void Update () {
		if (go)
			upd ();
	}

	public int nop_________nop;
	public int v_HereBeDragons;

	public enum TappaGameState {
		Inactive,
		Readying,
		Playing,
		LevelWin,
		LiterallyDying,
		GameOver
	};
	public TappaGameState state;
	public void stateSet(TappaGameState newstate) {
		TappaGameState oldstate = state;
		state = newstate;
		switch (newstate) {
			case TappaGameState.Inactive: break;
			case TappaGameState.Readying: 
				init ();
				break;
			//case TappaGameState.Readying: break;
			default: break;
		};
		stateTimer = 0;
	}

	int stateTimer = 0;
	int stateTimeReady = 2048;
	int stateTimeLevelWin = 2048*2;
	int stateTimeDying = 2048;
	int stateTimeGameOver = 2048*2;

	public int level = 0;
	public void levelWin() {
		stateSet (TappaGameState.LevelWin);
	}
	public void x_x() {
		Score.lives--;
		stateSet (TappaGameState.LiterallyDying);
	}

	public Mario itsame;
	public Bar[] bars;
	public int barslen = 0;
	public Pool<Beer> beers;
	public Pool<Peep> peeps;
	public Pool<Tip> tips;


	void create() {
				
		itsame = new Mario ();
		itsame.create ();

		bars = new Bar[barsmax];
		for (int i = 0; i < barsmax; i++) {
			Bar t = new Bar ();
			t.create ();
			bars[i] = t;
		}

		beers = new Pool<Beer>();
		beers.init (beersmax);
		for (int i = 0; i < beersmax; i++) {
			beers[i].create ();
			beers[i].getInThePool();
		}

		peeps = new Pool<Peep>();
		peeps.init (peepsmax);
		for (int i = 0; i < peepsmax; i++) {
			peeps[i].create ();
			peeps[i].getInThePool();
		}
		
		tips = new Pool<Tip>();
		tips.init (tipsmax);
		for (int i = 0; i < tipsmax; i++) {
			tips[i].create ();
			tips[i].getInThePool();
		}
		
		init ();

	}

	void init() {

		Score.clear ();
		barslen = data.bars.Length;
		for (int i = 0; i < barslen; i++) {
			Bar t = bars[i];
			t.init (data.bars[i]);
		}		
		itsame.init ();
		beers.poolParty ();
		peeps.poolParty ();
		tips.poolParty ();


	}

	void upd() {
		int x = (int)(Time.deltaTime*cfg.fps); while (x > 0) { x--;
			stateTimer++;
			bool anykey = false;
			anykey |= Input.GetKey (KeyCode.E);
			anykey |= Input.GetKey (KeyCode.S);
			anykey |= Input.GetKey (KeyCode.D);
			anykey |= Input.GetKey (KeyCode.F);

			if (state == TappaGameState.Inactive) {
				init ();
				stateSet(TappaGameState.Readying);
			} 
			else if (state == TappaGameState.Readying) {
				if (anykey)
				if (stateTimer >= stateTimeReady) stateSet(TappaGameState.Playing);			
			}
			else if (state == TappaGameState.Playing) {
				if (Mob.temporalSingularity != null) {
					Mob.temporalSingularity.GetType().InvokeMember("upd",System.Reflection.BindingFlags.InvokeMethod, null, Mob.temporalSingularity, null);
				} else {
					itsame.upd ();	
					for (int i = 0; i < barslen; i++)
						bars [i].upd ();
					for (int i = 0; i < beers.size; i++)
						beers [i].upd ();
					beers.poolCleaner();
					Tip.isdance = false;
					for (int i = 0; i < peeps.size; i++)
						peeps [i].upd ();
					peeps.poolCleaner();
					for (int i = 0; i < tips.size; i++)
						tips [i].upd ();
					tips.poolCleaner();

					if (Score.patrons >= Tappa.game.data.levelWinPeeps) {
						levelWin ();
					}
				}
			} 
			else if (state == TappaGameState.LevelWin) {
				if (anykey)
				if (stateTimer >= stateTimeLevelWin) {
					LevelSequence ls = gameObject.GetComponentInParent<LevelSequence>();
					ls.nextLevel();
					//level++;
					//stateSet(TappaGameState.Readying);
				}
			}
			else if (state == TappaGameState.LiterallyDying) {
				if (anykey)
				if (stateTimer >= stateTimeDying) stateSet(TappaGameState.Readying);
			}
			else if (state == TappaGameState.GameOver) {			
				if (anykey)
				if (stateTimer >= stateTimeGameOver) stateSet(TappaGameState.Readying);
			}



			if (x < cfg.f_stop) {
				//
			}
		}
	}

	//
}




public static class cfg {
	
	public static int fps = 3000;
	public static int f_stop = 64; 
	
	public static int hres = 1280;
	public static int vres = 1024;	
	
}